
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>ksnowlv</title>
  <meta name="author" content="律威">

  
  <meta name="description" content="2014年9月19日晚上，在阿里巴巴西溪园区里，一大帮土豪在舞台下，等待着公司上市的钟声在晚上9:30敲响. 现场气氛很热烈，不停地播放着排练的节目，虽然是微雨天，依然是人山人海;错过了成为土豪们的机会，却有幸和土豪们一起见证这个即将到来的时刻。 9:30的时刻，钟声，烟花，欢呼声,致词声& &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://ksnowlv.github.io/blog/page/5">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="ksnowlv" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="//ajax.useso.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.useso.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.useso.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">

  
  <script type="text/javascript">
    var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-39546615-2']);
    // _gaq.push(['_trackPageview']);

    // (function() {
    //   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    //   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    //   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    // })();

    _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src='http://droidyue-tools.qiniudn.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </script>


</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">ksnowlv</a></h1>
  
    <h2>回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:ksnowlv.github.io" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
  <li><a href="/">博客首页</a></li>
  <li><a href="/blog/archives">文章列表</a></li>
  <!-- <li><a href="/about-me">关于我</a></li>
 -->
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div class="blog-index">
  
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/22/hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/">杭州西溪园区九-九的夜晚</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-22T20:09:00+08:00" pubdate data-updated="true">Sep 22<span>nd</span>, 2014</time>
        
         | <a href="/blog/2014/09/22/hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>2014年9月19日晚上，在阿里巴巴西溪园区里，一大帮土豪在舞台下，等待着公司上市的钟声在晚上9:30敲响.</p>

<p>现场气氛很热烈，不停地播放着排练的节目，虽然是微雨天，依然是人山人海;错过了成为土豪们的机会，却有幸和土豪们一起见证这个即将到来的时刻。</p>

<p>9:30的时刻，钟声，烟花，欢呼声,致词声&hellip;</p>

<p>双十一，双十二，虽然还有一段时间，已经在争分夺秒地备战了。</p>

<p>阿里备战多年,今天只是水到渠成阶段性结果而已。</p>

<p>人要有梦想，万一实现了呢？</p>

<p><img src="/images/post/2014-09-22-hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/ali919_1.jpg" alt="image" />
<img src="/images/post/2014-09-22-hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/ali919_2.jpg" alt="image" />
<img src="/images/post/2014-09-22-hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/ali919_3.jpg" alt="image" />
<img src="/images/post/2014-09-22-hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/ali919_4.jpg" alt="image" />
<img src="/images/post/2014-09-22-hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/ali919_5.jpg" alt="image" />
<img src="/images/post/2014-09-22-hang-zhou-xi-xi-yuan-qu-jiu-yi-jiu-de-wan-shang/ali919_6.jpg" alt="image" /></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/13/swift-kuo-zhan/">Swift扩展</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-13T10:27:00+08:00" pubdate data-updated="true">Sep 13<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/13/swift-kuo-zhan/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>一.Swift中扩展</h3>

<p>语法格式，使用<code>extension</code>关键字修饰</p>

<h4>1.普通扩展</h4>

<p>  <code>extension</code> 扩展名{
  }</p>

<h4>2.对协议扩展</h4>

<p>  <code>extension</code> 扩展名: 协议1，协议2 {
}</p>

<h4>3.对类扩展</h4>

<p>  <code>extension</code> 扩展名: 类 {
}</p>

<h4>4.为扩展添加变量，使用运行时设置关联对象的方式。</h4>

<ul>
<li>1.声明运行时关联对象的key</li>
</ul>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">// 设置关联对象的key的定义</span>
</span><span class='line'><span class="n">var</span> <span class="nl">KUIViewExtraPropertySelector:</span><span class="n">Selector</span> <span class="o">=</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<ul>
<li>2.获取关联属性</li>
</ul>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">// 获取关联属性的对象</span>
</span><span class='line'><span class="n">func</span> <span class="n">getExtraPropertyFromObject</span><span class="p">(</span><span class="nl">object:</span> <span class="n">AnyObject</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">AnyObject</span><span class="o">?</span> <span class="p">{</span> <span class="c1">// Returns optional</span>
</span><span class='line'><span class="k">return</span> <span class="n">objc_getAssociatedObject</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">KUIViewExtraPropertySelector</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<ul>
<li>3.设置关联属性的对象,retain该属性的对象</li>
</ul>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">//设置关联属性的对象,retain该属性的对象</span>
</span><span class='line'><span class="n">func</span> <span class="nf">setExtraProperty</span><span class="p">(</span><span class="nl">property:</span> <span class="n">AnyObject</span><span class="p">,</span> <span class="nl">object:</span> <span class="n">AnyObject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'><span class="n">objc_setAssociatedObject</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">KUIViewExtraPropertySelector</span><span class="p">,</span> <span class="n">property</span><span class="p">,</span> <span class="n">UInt</span><span class="p">(</span><span class="n">OBJC_ASSOCIATION_RETAIN_NONATOMIC</span><span class="p">))</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>二.示例</h3>

<h4>1.代码</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">//设置关联对象的key的定义</span>
</span><span class='line'><span class="n">var</span> <span class="nl">KUIViewExtraPropertySelector:</span><span class="n">Selector</span> <span class="o">=</span> <span class="s">&quot;UIViewExtraPropertySelector&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="n">extension</span> <span class="n">UIView</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">showBackgroundColor</span><span class="p">(</span><span class="nl">color:</span><span class="n">UIColor</span><span class="p">){</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">backgroundColor</span> <span class="o">=</span> <span class="n">color</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;backgroundColor:\(color.description)&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// 获取关联属性的对象</span>
</span><span class='line'>    <span class="n">func</span> <span class="n">getExtraPropertyFromObject</span><span class="p">(</span><span class="nl">object:</span> <span class="n">AnyObject</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">AnyObject</span><span class="o">?</span> <span class="p">{</span> <span class="c1">// Returns optional</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">objc_getAssociatedObject</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">KUIViewExtraPropertySelector</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//设置关联属性的对象,retain该属性的对象</span>
</span><span class='line'>    <span class="n">func</span> <span class="n">setExtraProperty</span><span class="p">(</span><span class="nl">property:</span> <span class="n">AnyObject</span><span class="p">,</span> <span class="nl">object:</span> <span class="n">AnyObject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">objc_setAssociatedObject</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">KUIViewExtraPropertySelector</span><span class="p">,</span> <span class="n">property</span><span class="p">,</span> <span class="n">UInt</span><span class="p">(</span><span class="n">OBJC_ASSOCIATION_RETAIN_NONATOMIC</span><span class="p">))</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>2.调用</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">self</span><span class="p">.</span><span class="n">view</span><span class="p">.</span><span class="n">showBackgroundColor</span><span class="p">(</span><span class="n">UIColor</span><span class="p">.</span><span class="n">redColor</span><span class="p">())</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">var</span> <span class="nl">button:</span><span class="n">UIButton</span> <span class="o">=</span> <span class="n">UIButton</span><span class="p">.</span><span class="n">buttonWithType</span><span class="p">(</span><span class="n">UIButtonType</span><span class="p">.</span><span class="n">DetailDisclosure</span><span class="p">)</span> <span class="n">as</span> <span class="n">UIButton</span>
</span><span class='line'>
</span><span class='line'><span class="n">var</span>  <span class="nl">buttonRect:</span><span class="n">CGRect</span> <span class="o">=</span> <span class="n">button</span><span class="p">.</span><span class="n">bounds</span>
</span><span class='line'><span class="n">buttonRect</span><span class="p">.</span><span class="n">origin</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="n">CGRectGetWidth</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">view</span><span class="p">.</span><span class="n">frame</span><span class="p">)</span> <span class="o">-</span> <span class="n">CGRectGetWidth</span><span class="p">(</span><span class="n">buttonRect</span><span class="p">))</span><span class="o">/</span><span class="mi">2</span>
</span><span class='line'><span class="n">buttonRect</span><span class="p">.</span><span class="n">origin</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="mi">50</span>
</span><span class='line'>
</span><span class='line'><span class="n">button</span><span class="p">.</span><span class="n">frame</span> <span class="o">=</span> <span class="n">buttonRect</span>
</span><span class='line'><span class="n">self</span><span class="p">.</span><span class="n">view</span><span class="p">.</span><span class="n">addSubview</span><span class="p">(</span><span class="n">button</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">button</span><span class="p">.</span><span class="n">setExtraProperty</span><span class="p">(</span><span class="s">&quot;string&quot;</span><span class="p">,</span> <span class="nl">object:</span> <span class="s">&quot;ksnowlv&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">var</span> <span class="nl">viewObject:</span> <span class="n">AnyObject</span> <span class="o">=</span> <span class="n">button</span><span class="p">.</span><span class="n">getExtraPropertyFromObject</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">)</span><span class="o">!</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="n">viewObject</span> <span class="n">is</span>  <span class="n">String</span><span class="p">{</span>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="s">&quot;object is String&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">else</span><span class="p">{</span>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="s">&quot;object is not String&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">var</span> <span class="nl">list:</span><span class="n">Array</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;kair&quot;</span><span class="p">,</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span><span class="s">&quot;snow&quot;</span><span class="p">,</span><span class="s">&quot;air&quot;</span><span class="p">,</span><span class="s">&quot;rain&quot;</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">button</span><span class="p">.</span><span class="n">setExtraProperty</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="nl">object:</span> <span class="s">&quot;ksnowlv&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">var</span> <span class="nl">listObject:</span><span class="n">AnyObject</span> <span class="o">=</span> <span class="n">button</span><span class="p">.</span><span class="n">getExtraPropertyFromObject</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">)</span><span class="o">!</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="n">listObject</span> <span class="n">is</span> <span class="n">Array</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="p">{</span>
</span><span class='line'>     <span class="n">println</span><span class="p">(</span><span class="s">&quot;object is [Array&lt;String&gt;] \(listObject.description)&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">else</span><span class="p">{</span>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="s">&quot;object is not [Array&lt;String&gt;]&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>3.日志</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="nl">backgroundColor:</span><span class="n">UIDeviceRGBColorSpace</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span>
</span><span class='line'><span class="n">object</span> <span class="n">is</span> <span class="n">String</span>
</span><span class='line'><span class="n">object</span> <span class="n">is</span> <span class="p">[</span><span class="n">Array</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span><span class="p">]</span> <span class="p">(</span>
</span><span class='line'>    <span class="n">kair</span><span class="p">,</span>
</span><span class='line'>    <span class="n">ksnowlv</span><span class="p">,</span>
</span><span class='line'>    <span class="n">snow</span><span class="p">,</span>
</span><span class='line'>    <span class="n">air</span><span class="p">,</span>
</span><span class='line'>    <span class="n">rain</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<h3>三.小结</h3>

<blockquote><p>大体上使用方法与原<code>objective-c</code>类似</p></blockquote>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/12/swift-zi-dong-yin-yong-ji-shu/">Swift自动引用计数</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-12T14:43:00+08:00" pubdate data-updated="true">Sep 12<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/12/swift-zi-dong-yin-yong-ji-shu/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>一.swift中自动引用计数</h3>

<h4>1.概况</h4>

<p><code>Swift</code>使用自动引用计数(<code>ARC</code>)来跟踪并管理应用使用的内存。当实例不再被使用时，ARC会自动释放这些类的实例所占用的内存。</p>

<blockquote><p>引用计数只应用在类的实例对象中。</p>

<p>结构体(<code>Structure</code>)和枚举类型是值传递类型的，并非引用类型，不是以引用的方式来存储和传递的</p></blockquote>

<h4>2.<code>swift</code>中容易导致内存泄漏的两种场景</h4>

<p>1.类实例间的强引用环</p>

<p> 解决办法</p>

<h4>a.弱引用，<code>weak</code>关键字修饰</h4>

<p>   弱引用不会增加实例变量的引用计数，因此不会阻止ARC销毁被引用的实例。</p>

<p>   这种特性使得引用不会变成强引用环。声明属性或者变量的时候，关键字weak表明引用为弱引用</p>

<blockquote><p>弱引用只能声明为变量类型，因为运行时它的值可能改变。弱引用绝对不能声明为常量</p></blockquote>

<h4>b.无主引用，<code>unowned</code>关键字修饰</h4>

<p>  在属性、变量前添加unowned关键字，可以声明一个无主引用</p>

<p>  无主引用也不强持有实例。无主引用默认始终有值。</p>

<p>  无主引用只能定义为非可选类型（non-optional type），因为是非可选类型，因此当使用无主引用的时候，不需要展开，可以直接访问。不过非可选类型变量不能赋值为nil，因此当实例被销毁的时候，ARC无法将引用赋值为nil</p>

<blockquote><p>当实例被销毁后，试图访问该实例的无主引用会触发运行时错误。使用无主引用时请确保引用始终指向一个未销毁的实例。 上面的非法操作会百分百让应用崩溃，不会发生无法预期的行为。</p></blockquote>

<h4>c.解除对象的强引用</h4>

<ul>
<li>2.闭包产生的强引用环</li>
</ul>


<p><code>Swift</code>解除闭包和实例对象之间的强引用环的方法:闭包占用列表(closuer capture list)</p>

<h3>二.示例</h3>

<h4>1.代码</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">class</span> <span class="n">QClassA</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_classB:</span><span class="n">QClassB</span><span class="o">?</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_classBB:</span><span class="n">QClassB</span><span class="o">?</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_classC:</span><span class="n">QClassC</span><span class="o">?</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassA init&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">deinit</span><span class="p">{</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassA deinit&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">class</span> <span class="n">QClassB</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_classA:</span><span class="n">QClassA</span><span class="o">?</span>
</span><span class='line'>    <span class="c1">//使用weak</span>
</span><span class='line'>    <span class="n">weak</span> <span class="n">var</span> <span class="nl">_classAA:</span><span class="n">QClassA</span><span class="o">?</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassB init&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(</span><span class="nl">classA:</span> <span class="n">QClassA</span><span class="p">){</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassB init&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">deinit</span><span class="p">{</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassB deinit&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">class</span> <span class="n">QClassC</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">//使用无主引用</span>
</span><span class='line'>    <span class="n">unowned</span> <span class="n">let</span>  <span class="nl">_classA:</span><span class="n">QClassA</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(</span><span class="nl">classA:</span> <span class="n">QClassA</span><span class="p">){</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassC init&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">_classA</span> <span class="o">=</span> <span class="n">classA</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">deinit</span><span class="p">{</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassC deinit&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">///实例和闭包</span>
</span><span class='line'><span class="n">class</span> <span class="n">QClassD</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_value:</span><span class="n">String</span><span class="o">?</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="n">lazy</span> <span class="n">var</span> <span class="nl">_funcBlockA:</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Void</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>        <span class="c1">//去掉下面[unowned self]，当调用_funcBlockA时，</span>
</span><span class='line'>        <span class="c1">//会导致block和QClassD实例对象相互强引用</span>
</span><span class='line'>        <span class="p">[</span><span class="n">unowned</span> <span class="n">self</span><span class="p">]</span>  <span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Void</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">self</span><span class="p">.</span><span class="n">_value</span> <span class="o">!=</span> <span class="nb">nil</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">println</span><span class="p">(</span><span class="s">&quot; ***_funcBlockA value =  \(self._value) &quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="k">else</span><span class="p">{</span>
</span><span class='line'>           <span class="n">println</span><span class="p">(</span><span class="s">&quot; ***_funcBlockA value =  \(self._value) &quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassD init&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">deinit</span><span class="p">{</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;QClassD deinit&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.调用</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">//解除对象的强引用 </span>
</span><span class='line'><span class="n">var</span> <span class="nl">a:</span><span class="n">QClassA</span> <span class="o">=</span> <span class="n">QClassA</span><span class="p">()</span>
</span><span class='line'><span class="n">var</span> <span class="nl">b:</span><span class="n">QClassB</span> <span class="o">=</span> <span class="n">QClassB</span><span class="p">()</span>
</span><span class='line'><span class="n">a</span><span class="p">.</span><span class="n">_classB</span> <span class="o">=</span> <span class="n">b</span>
</span><span class='line'><span class="n">b</span><span class="p">.</span><span class="n">_classA</span> <span class="o">=</span> <span class="n">a</span>
</span><span class='line'>
</span><span class='line'><span class="n">a</span><span class="p">.</span><span class="n">_classB</span> <span class="o">=</span> <span class="nb">nil</span><span class="c1">//或b._classA = nil</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//weak</span>
</span><span class='line'><span class="n">var</span> <span class="nl">aa:</span><span class="n">QClassA</span> <span class="o">=</span> <span class="n">QClassA</span><span class="p">()</span>
</span><span class='line'><span class="n">var</span> <span class="nl">bb:</span><span class="n">QClassB</span> <span class="o">=</span> <span class="n">QClassB</span><span class="p">()</span>
</span><span class='line'><span class="n">aa</span><span class="p">.</span><span class="n">_classBB</span> <span class="o">=</span> <span class="n">bb</span>
</span><span class='line'><span class="n">bb</span><span class="p">.</span><span class="n">_classAA</span> <span class="o">=</span> <span class="n">aa</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//无主引用</span>
</span><span class='line'><span class="n">var</span> <span class="nl">aaa:</span><span class="n">QClassA</span> <span class="o">=</span> <span class="n">QClassA</span><span class="p">()</span>
</span><span class='line'><span class="n">var</span> <span class="nl">ccc:</span><span class="n">QClassC</span> <span class="o">=</span> <span class="n">QClassC</span><span class="p">(</span><span class="nl">classA:</span> <span class="n">aaa</span><span class="p">)</span>
</span><span class='line'><span class="n">aa</span><span class="p">.</span><span class="n">_classC</span> <span class="o">=</span> <span class="n">ccc</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//解除闭包和实例之间的强引用</span>
</span><span class='line'><span class="n">var</span> <span class="nl">d:</span><span class="n">QClassD</span> <span class="o">=</span> <span class="n">QClassD</span><span class="p">()</span>
</span><span class='line'><span class="n">d</span><span class="p">.</span><span class="n">_funcBlockA</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<h4>3.日志</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">QClassA</span> <span class="n">init</span>
</span><span class='line'><span class="n">QClassB</span> <span class="n">init</span>
</span><span class='line'><span class="n">QClassA</span> <span class="n">init</span>
</span><span class='line'><span class="n">QClassB</span> <span class="n">init</span>
</span><span class='line'><span class="n">QClassA</span> <span class="n">init</span>
</span><span class='line'><span class="n">QClassC</span> <span class="n">init</span>
</span><span class='line'><span class="n">QClassD</span> <span class="n">init</span>
</span><span class='line'> <span class="o">***</span><span class="n">_funcBlockA</span> <span class="n">value</span> <span class="o">=</span>  <span class="nb">nil</span>
</span><span class='line'><span class="n">QClassD</span> <span class="n">deinit</span>
</span><span class='line'><span class="n">QClassA</span> <span class="n">deinit</span>
</span><span class='line'><span class="n">QClassA</span> <span class="n">deinit</span>
</span><span class='line'><span class="n">QClassB</span> <span class="n">deinit</span>
</span><span class='line'><span class="n">QClassC</span> <span class="n">deinit</span>
</span><span class='line'><span class="n">QClassB</span> <span class="n">deinit</span>
</span><span class='line'><span class="n">QClassA</span> <span class="n">deinit</span>
</span></code></pre></td></tr></table></div></figure>


<h3>三.小结</h3>

<p><code>swift</code>与<code>objective-c</code></p>

<ul>
<li>1.都是通过引用计数来管理内存的。</li>
<li>2.都存在循环引用导致的内存泄漏，需要注意发生这种内存泄漏的场景。</li>
<li>3.闭包都存在强引用环，需要打破引用环。</li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/11/swift-bi-bao/">Swift闭包</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-11T19:46:00+08:00" pubdate data-updated="true">Sep 11<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/11/swift-bi-bao/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>一.swift中闭包</h3>

<p>swift中的闭包格式</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="p">{</span> <span class="p">(</span><span class="n">parameters</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="k">return</span> <span class="n">type</span> <span class="k">in</span>
</span><span class='line'>    <span class="n">statements</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>1. swift block, without any arguments and without return value</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'> <span class="n">typealias</span> <span class="n">funcBlockA</span> <span class="o">=</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="p">()</span>
</span><span class='line'> <span class="n">typealias</span> <span class="n">funcBlockAA</span> <span class="o">=</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Void</span>
</span><span class='line'>
</span><span class='line'><span class="n">var</span> <span class="nl">_funcBlockA:</span><span class="n">funcBlockA</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Void</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="s">&quot;***block without any arguments and without return value&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.swift block with arguments and  return value</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="nl">_funcBlockB:</span><span class="n">funcBlockB</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">(</span><span class="nl">stringA:</span><span class="n">String</span><span class="p">,</span> <span class="nl">stringB:</span><span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span><span class="n">String</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="s">&quot;***swift block with arguments and without any return value&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="p">(</span><span class="n">stringA</span> <span class="o">+</span> <span class="n">stringB</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>3.swift block with arguments and  return value as function</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="nl">_funcBlockC:</span><span class="n">funcBlockC</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">(</span><span class="nl">stringA:</span><span class="n">String</span><span class="p">,</span> <span class="nl">stringB:</span><span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span><span class="p">(</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="p">()</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">blockC</span><span class="p">(</span><span class="s">&quot;***wift block with arguments and  return value:\(stringA) and \(stringB)&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">blockC</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>4.swift block as function parameter and  return value as function with parameter and return value</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="nl">_funcBlockD:</span><span class="n">funcBlockD</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">(</span><span class="nl">stringA:</span><span class="n">String</span><span class="p">,</span> <span class="nl">stringB:</span><span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span><span class="p">(</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="p">(</span><span class="n">String</span><span class="p">)</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">blockD</span><span class="p">(</span><span class="s">&quot;***swift block as function parameter:\(stringA) and \(stringB)&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">blockD</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h3>二.示例</h3>

<h4>1.示例代码</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">func</span> <span class="nf">blockC</span><span class="p">(</span><span class="nl">name:</span><span class="n">String</span><span class="p">)</span>  <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="s">&quot; blockC= (\(name))&quot;</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">func</span> <span class="n">blockD</span><span class="p">(</span><span class="nl">name:</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">String</span>  <span class="p">{</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">result:</span><span class="n">String</span> <span class="o">=</span> <span class="s">&quot;blockD = &quot;</span> <span class="o">+</span> <span class="n">name</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">println</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">class</span> <span class="n">QBlock</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//swift block, without any arguments and without return value</span>
</span><span class='line'>    <span class="n">typealias</span> <span class="n">funcBlockA</span> <span class="o">=</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="p">()</span>
</span><span class='line'>    <span class="n">typealias</span> <span class="n">funcBlockAA</span> <span class="o">=</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Void</span>
</span><span class='line'>    <span class="c1">//swift block with arguments and return value</span>
</span><span class='line'>    <span class="n">typealias</span> <span class="n">funcBlockB</span> <span class="o">=</span> <span class="p">(</span><span class="n">String</span><span class="p">,</span> <span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">String</span>
</span><span class='line'>    <span class="c1">//swift block with arguments and  return value as function</span>
</span><span class='line'>    <span class="n">typealias</span> <span class="n">funcBlockC</span> <span class="o">=</span> <span class="p">(</span><span class="n">String</span><span class="p">,</span> <span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="p">()</span>
</span><span class='line'>    <span class="c1">//swift block as function parameter and  return value as function with parameter and return value</span>
</span><span class='line'>    <span class="n">typealias</span> <span class="n">funcBlockD</span> <span class="o">=</span> <span class="p">(</span><span class="n">String</span><span class="p">,</span> <span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">String</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_funcBlockA:</span><span class="n">funcBlockA</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Void</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;***block without any arguments and without return value&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_funcBlockB:</span><span class="n">funcBlockB</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>        <span class="p">(</span><span class="nl">stringA:</span><span class="n">String</span><span class="p">,</span> <span class="nl">stringB:</span><span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span><span class="n">String</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;***swift block with arguments and without any return value&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="p">(</span><span class="n">stringA</span> <span class="o">+</span> <span class="n">stringB</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_funcBlockC:</span><span class="n">funcBlockC</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="p">(</span><span class="nl">stringA:</span><span class="n">String</span><span class="p">,</span> <span class="nl">stringB:</span><span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span><span class="p">(</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="p">()</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">blockC</span><span class="p">(</span><span class="s">&quot;***wift block with arguments and  return value:\(stringA) and \(stringB)&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">blockC</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_funcBlockD:</span><span class="n">funcBlockD</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>        <span class="p">(</span><span class="nl">stringA:</span><span class="n">String</span><span class="p">,</span> <span class="nl">stringB:</span><span class="n">String</span><span class="p">)</span> <span class="o">-&gt;</span><span class="p">(</span><span class="n">String</span><span class="p">)</span><span class="o">-&gt;</span><span class="p">(</span><span class="n">String</span><span class="p">)</span> <span class="k">in</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">blockD</span><span class="p">(</span><span class="s">&quot;***swift block as function parameter:\(stringA) and \(stringB)&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">blockD</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">testMemberBlock</span><span class="p">()</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>   <span class="n">private</span> <span class="n">func</span> <span class="n">testMemberBlock</span><span class="p">(){</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">///1. swift block, without any arguments and without return value</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">_funcBlockA</span><span class="p">()</span>
</span><span class='line'>        <span class="c1">//swift block with arguments and without any return value</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">res</span> <span class="o">=</span>  <span class="n">self</span><span class="p">.</span><span class="n">_funcBlockB</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span> <span class="s">&quot;kair&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;_funcBlockB = result = \(res)&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// //swift block with arguments and  return value as function</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">blockC</span>  <span class="o">=</span> <span class="n">self</span><span class="p">.</span><span class="n">_funcBlockC</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span> <span class="s">&quot;kair&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="n">blockC</span><span class="p">(</span><span class="s">&quot;go shopping&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">//swift block as function parameter and  return value as function with parameter and return value</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">blockD</span>  <span class="o">=</span> <span class="n">self</span><span class="p">.</span><span class="n">_funcBlockD</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span> <span class="s">&quot;kair&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="n">var</span> <span class="n">result</span> <span class="o">=</span>  <span class="n">blockD</span><span class="p">(</span><span class="s">&quot;go shopping&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;result blockD = \(result)&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.调用</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="n">block</span> <span class="o">=</span> <span class="n">QBlock</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>objective-c</p>

<h4>3.日志</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="o">***</span><span class="n">block</span> <span class="n">without</span> <span class="n">any</span> <span class="n">arguments</span> <span class="n">and</span> <span class="n">without</span> <span class="k">return</span> <span class="n">value</span>
</span><span class='line'><span class="o">***</span><span class="n">swift</span> <span class="n">block</span> <span class="n">with</span> <span class="n">arguments</span> <span class="n">and</span> <span class="n">without</span> <span class="n">any</span> <span class="k">return</span> <span class="n">value</span>
</span><span class='line'><span class="n">_funcBlockB</span> <span class="o">=</span> <span class="n">result</span> <span class="o">=</span> <span class="n">ksnowlvkair</span>
</span><span class='line'> <span class="n">blockC</span><span class="o">=</span> <span class="p">(</span><span class="o">***</span><span class="n">wift</span> <span class="n">block</span> <span class="n">with</span> <span class="n">arguments</span> <span class="n">and</span>  <span class="k">return</span> <span class="nl">value:</span><span class="n">ksnowlv</span> <span class="n">and</span> <span class="n">kair</span><span class="p">)</span>
</span><span class='line'> <span class="n">blockC</span><span class="o">=</span> <span class="p">(</span><span class="n">go</span> <span class="n">shopping</span><span class="p">)</span>
</span><span class='line'><span class="n">blockD</span> <span class="o">=</span> <span class="o">***</span><span class="n">swift</span> <span class="n">block</span> <span class="n">as</span> <span class="n">function</span> <span class="nl">parameter:</span><span class="n">ksnowlv</span> <span class="n">and</span> <span class="n">kair</span>
</span><span class='line'><span class="n">blockD</span> <span class="o">=</span> <span class="n">go</span> <span class="n">shopping</span>
</span><span class='line'><span class="n">result</span> <span class="n">blockD</span> <span class="o">=</span> <span class="n">blockD</span> <span class="o">=</span> <span class="n">go</span> <span class="n">shopping</span>
</span></code></pre></td></tr></table></div></figure>


<h3>三.小结</h3>

<ul>
<li>1.与objective-c中的闭包在使用方法上类似，大体上相同，语法格式上的差异。</li>
<li>2.在内存管理方面:objective-c中需要注意循环引用导致的内存泄漏。而swift依然需要注意该问题。</li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/11/swift-wei-tuo/">Swift委托</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-11T13:59:00+08:00" pubdate data-updated="true">Sep 11<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/11/swift-wei-tuo/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>swift中的委托和objective-c中的类似。</p>

<h3>一.协议支持required/optional</h3>

<p>如果协议是optional协议</p>

<h4>1.协议接口存在与否的判断,</h4>

<p> 调用方法、属性、下标索引等前面需要加上一个?</p>

<blockquote><p>1.如果是nil值，也就是Optional.None，会跳过后面的操作不执行;</p>

<p>2.如果有值，就是Optional.Some，就会拆包(unwrap)，然后对拆包后的值执行后面的操作，来保证执行这个操作的安全性.</p></blockquote>

<h4>2.声明与必选协议的不同之处。可选协议需要@objc</h4>

<h3>二.示例</h3>

<p>1.完整示例代码</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="cm">/**</span>
</span><span class='line'><span class="cm">游戏状态</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'><span class="k">enum</span> <span class="n">QQGameState</span> <span class="o">:</span><span class="n">Int</span><span class="p">{</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">QQGameStateNone</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">QQGameStateStart</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">QQGameStatePlaying</span> <span class="o">=</span> <span class="mi">2</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">QQGameStatePause</span> <span class="o">=</span> <span class="mi">3</span>
</span><span class='line'>    <span class="k">case</span> <span class="n">QQGameStateGameOver</span> <span class="o">=</span> <span class="mi">4</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="cm">/**</span>
</span><span class='line'><span class="cm">游戏Delegate</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'><span class="err">@</span><span class="n">objc</span> <span class="n">protocol</span> <span class="n">QQGameDelegate</span><span class="p">{</span>
</span><span class='line'>    <span class="n">func</span> <span class="n">notifyPlayerState</span><span class="p">()</span>
</span><span class='line'>    <span class="n">optional</span> <span class="n">func</span> <span class="n">notifyGameState</span><span class="p">(</span><span class="nl">gameState:</span> <span class="n">Int</span><span class="p">)</span>
</span><span class='line'>    <span class="n">optional</span> <span class="n">func</span> <span class="n">notifyGameNotification</span><span class="p">(</span><span class="nl">gameNotification:</span> <span class="n">String</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="cm">/**</span>
</span><span class='line'><span class="cm">游戏事件类</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'><span class="n">class</span> <span class="nl">QQGameStateEvent:</span><span class="n">QQGameDelegate</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">notifyPlayerState</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;notifyPlayerState!!!&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">notifyGameNotification</span><span class="p">(</span><span class="nl">gameNotification:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;game notification : \(gameNotification)&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="cm">/**</span>
</span><span class='line'><span class="cm">游戏类</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'><span class="n">class</span> <span class="n">QQGame</span><span class="p">{</span>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">_gameState:</span><span class="n">QQGameState</span> <span class="o">=</span> <span class="n">QQGameState</span><span class="p">.</span><span class="n">QQGameStateNone</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">_gameDelegate:</span><span class="n">QQGameDelegate</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">gameDelegate:</span><span class="n">QQGameDelegate</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">_gameDelegate</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="n">set</span><span class="p">{</span>
</span><span class='line'>            <span class="n">self</span><span class="p">.</span><span class="n">_gameDelegate</span> <span class="o">=</span> <span class="n">newValue</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">_gameDelegate</span> <span class="o">=</span> <span class="n">QQGameStateEvent</span><span class="p">();</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">gameRunning</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">gameRunning</span><span class="p">(){</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">//game start</span>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyGameState</span><span class="o">?</span><span class="p">(</span><span class="n">QQGameState</span><span class="p">.</span><span class="n">QQGameStateStart</span><span class="p">.</span><span class="n">toRaw</span><span class="p">());</span>
</span><span class='line'>        <span class="c1">//game playing</span>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyGameState</span><span class="o">?</span><span class="p">(</span><span class="n">QQGameState</span><span class="p">.</span><span class="n">QQGameStatePlaying</span><span class="p">.</span><span class="n">toRaw</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">//game pause</span>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyGameState</span><span class="o">?</span><span class="p">(</span><span class="n">QQGameState</span><span class="p">.</span><span class="n">QQGameStatePause</span><span class="p">.</span><span class="n">toRaw</span><span class="p">());</span>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyGameNotification</span><span class="o">?</span><span class="p">(</span><span class="s">&quot;游戏停服更新!!!&quot;</span><span class="p">);</span>
</span><span class='line'>        <span class="c1">//Game Over</span>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyGameState</span><span class="o">?</span><span class="p">(</span><span class="n">QQGameState</span><span class="p">.</span><span class="n">QQGameStateGameOver</span><span class="p">.</span><span class="n">toRaw</span><span class="p">());</span>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyGameNotification</span><span class="o">?</span><span class="p">(</span><span class="s">&quot;系统检测到你使用外挂，封号七天!!!&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">_gameDelegate</span><span class="p">.</span><span class="n">notifyPlayerState</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.调用</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="n">game</span> <span class="o">=</span>  <span class="n">QQGame</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<h4>3.日志</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">game</span> <span class="n">notification</span> <span class="o">:</span> <span class="err">游戏停服更新</span><span class="o">!!!</span>
</span><span class='line'><span class="n">game</span> <span class="n">notification</span> <span class="o">:</span> <span class="err">系统检测到你使用外挂，封号七天</span><span class="o">!!!</span>
</span><span class='line'><span class="n">notifyPlayerState</span>
</span></code></pre></td></tr></table></div></figure>


<h3>三.小结</h3>

<h4>1.?的使用场景:</h4>

<ul>
<li>1.声明Optional值变量</li>
<li>2.在对Optional值操作中，用来判断是否能响应后面的操作</li>
<li>3.用于安全调用protocol的optional方法</li>
<li>4.使用 as? 向下转型(Downcast)</li>
</ul>


<h4>2.!使用场景</h4>

<p>1.强制对Optional值进行拆包(unwrap)
2.声明Implicitly Unwrapped Optionals值，一般用于类中的属性</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="err">@</span><span class="kt">IBOutlet</span> <span class="n">weak</span> <span class="n">var</span> <span class="nl">tableView:</span><span class="n">UITableView</span><span class="o">!</span>
</span><span class='line'><span class="err">@</span><span class="kt">IBOutlet</span> <span class="n">weak</span>  <span class="n">var</span> <span class="nl">nameLabel:</span> <span class="n">ImplicitlyUnwrappedOptional</span><span class="o">&lt;</span><span class="n">UILabel</span><span class="o">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h4>3.和objective-c协议用法类似，仅仅是细节上的差异。</h4>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/10/swift-xie-yi/">Swift协议</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-10T20:12:00+08:00" pubdate data-updated="true">Sep 10<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/10/swift-xie-yi/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>一.swift协议格式</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">protocol</span> <span class="err">协议名</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// protocol </span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>1.协议支持属性</h4>

<blockquote><ul>
<li>要求声明为变量属性，用var关键字做前缀。</li>
<li>支持setter/getter</li>
</ul>


<p>其格式</p></blockquote>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">protocol</span> <span class="err">协议名</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">var</span> <span class="err">属性名</span><span class="o">:</span> <span class="err">类型</span> <span class="p">{</span> <span class="n">get</span> <span class="n">set</span> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.协议支持方法(普通方法/类方法)</h4>

<p>与类的普通方法和类方法声明语法相同，只是不用添加实现体。</p>

<pre><code>协议是不允许给方法参数指定默认值。
</code></pre>

<h4>3.协议支持Mutating方法</h4>

<p>   对值类型实例方法（即结构和枚举），你将可以将mutating关键字放在方法func关键字之前，表明该方法允许修改所属实例和/或实例的任何属性。</p>

<blockquote><p>1.如果你标记一个协议实例方法为mutating， 当为一个类写此方法的实现时，你不需要写mutating关键字。</p>

<p>2.mutating关键字只用于结构和枚举。</p></blockquote>

<h4>4.协议可以组合，但是不会产生新协议</h4>

<p>格式</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">protocol</span><span class="o">&lt;</span><span class="err">协议</span><span class="mi">1</span><span class="p">,</span> <span class="err">协议</span><span class="mi">2</span><span class="err">，</span><span class="p">...,</span><span class="err">协议</span><span class="n">n</span><span class="o">&gt;</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>5.协议支持required/optional</h4>

<p>如果协议是optional协议</p>

<blockquote><p>1.协议接口存在与否的判断</p>

<p>2.声明与必选协议的不同之处。可选协议需要@objc</p></blockquote>

<h3>二.参考示例。</h3>

<h4>1.完整示例</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">protocol</span> <span class="n">QBaseClassProtocol</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">className:</span><span class="n">String</span><span class="p">{</span><span class="n">get</span><span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="err">@</span><span class="n">objc</span> <span class="n">protocol</span> <span class="n">QOutActionProtocol</span><span class="p">{</span>
</span><span class='line'>    <span class="n">optional</span> <span class="n">func</span> <span class="n">gotoPark</span><span class="p">()</span> <span class="o">-&gt;</span><span class="n">Bool</span>
</span><span class='line'>    <span class="n">optional</span> <span class="n">var</span> <span class="nl">goShopping:</span><span class="n">Bool</span><span class="p">{</span><span class="n">get</span><span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">class</span> <span class="n">QBaseClass</span> <span class="o">:</span><span class="n">QBaseClassProtocol</span><span class="p">,</span> <span class="n">QOutActionProtocol</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">_className:</span><span class="n">String</span> <span class="o">=</span> <span class="s">&quot;QBaseClass&quot;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//from QBaseClassProtocol</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">className:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">_className</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//from QOutActionProtocol</span>
</span><span class='line'>    <span class="n">func</span> <span class="n">gotoPark</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">Bool</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">true</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">test</span><span class="p">(){</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">println</span><span class="p">(</span><span class="s">&quot;className = \(_className)&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">var</span> <span class="nl">gotoPark:</span><span class="n">Bool</span>  <span class="o">=</span> <span class="n">self</span><span class="p">.</span><span class="n">gotoPark</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">gotoPark</span><span class="p">{</span>
</span><span class='line'>            <span class="n">println</span><span class="p">(</span><span class="s">&quot;I can go to park&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="k">else</span><span class="p">{</span>
</span><span class='line'>            <span class="n">println</span><span class="p">(</span><span class="s">&quot;I can&#39;t go to park&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="c1">/// &#39;QBaseClass&#39; does not have a member named &#39;goShopping&#39;</span>
</span><span class='line'>        <span class="c1">//self.goShopping()</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<h4>2.调用</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="n">baseClass</span> <span class="o">=</span>  <span class="n">QBaseClass</span><span class="p">()</span>
</span><span class='line'><span class="n">baseClass</span><span class="p">.</span><span class="n">test</span><span class="p">();</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>3.日志</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">className</span> <span class="o">=</span> <span class="n">QBaseClass</span>
</span><span class='line'><span class="n">I</span> <span class="n">can</span> <span class="n">go</span> <span class="n">to</span> <span class="n">park</span>
</span></code></pre></td></tr></table></div></figure>


<h3>三.小结</h3>

<p><code>swift</code>中协议大体与<code>objective-c</code>协议相同。主要区别点</p>

<ul>
<li><ol>
<li><code>swift</code>支持属性，<code>objective-c</code>而不支持。</li>
</ol>
</li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/10/charles-jie-bao-gong-ju/">Charles截包工具</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-10T16:27:00+08:00" pubdate data-updated="true">Sep 10<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/10/charles-jie-bao-gong-ju/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><a href="http://www.charlesproxy.com/">Charles</a> 是一款<code>Mac</code>上好用的截包工具,可以让开发者查看所有连接互联网的<code>HTTP</code>通信，包括请求，响应和<code>HTTP</code>头信息等等,非常不错!</p>

<p><img src="/images/post/2014-09-10-charles-jie-bao-gong-ju/charles_ui_overview.png" alt="image" /></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/10/swift-shu-xing-fang-wen/">Swift属性访问</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-10T10:35:00+08:00" pubdate data-updated="true">Sep 10<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/10/swift-shu-xing-fang-wen/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p><code>objective-c</code>中<code>property</code>方法支持<code>assign/readonly/retain/readwrite</code>等。
<code>swift</code>也支持类似这些属性。</p>

<h3>一.swift属性方法</h3>

<h4>1.readonly</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="nl">name:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>    <span class="n">get</span><span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">name</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>2.read/write with backing store</h4>

<p>如果发生<code>EXC_BAD_ACCESS</code>参考<a href="http://stackoverflow.com/questions/24025340/property-getters-and-setters">http://stackoverflow.com/questions/24025340/property-getters-and-setters</a></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">//read/write Setters/Getters用来计算和获取属性，</span>
</span><span class='line'><span class="c1">//并没有给成员变量实例分配存储,所以，会触发EXC_BAD_ACCESS.</span>
</span><span class='line'><span class="n">private</span> <span class="n">var</span> <span class="nl">realAge:</span><span class="n">Int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="n">var</span> <span class="nl">age:</span><span class="n">Int</span><span class="p">{</span>
</span><span class='line'>    <span class="n">get</span><span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">realAge</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">set</span><span class="p">{</span>
</span><span class='line'>        <span class="n">realAge</span> <span class="o">=</span> <span class="n">newValue</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>3.read/write  with backing store,信息隐藏，监控属性变化，附加额外处理</h4>

<p>类似</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'>
</span><span class='line'> <span class="k">@property</span><span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">retain</span><span class="p">)</span><span class="n">NSString</span> <span class="n">address</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>
  实例变量自动为_address</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">private</span> <span class="n">var</span> <span class="n">_address</span> <span class="o">:</span><span class="n">String</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">{</span>
</span><span class='line'>    <span class="n">willSet</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>          <span class="n">println</span><span class="p">(</span><span class="s">&quot;old Value = \(_address) will clear,new value = \(newValue) will set&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">_address</span> <span class="o">=</span> <span class="n">newValue</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="n">didSet</span><span class="p">{</span>
</span><span class='line'>        <span class="c1">//可以追回一些额外的处理逻辑</span>
</span><span class='line'>         <span class="n">println</span><span class="p">(</span><span class="s">&quot;Property did set&quot;</span><span class="p">)</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">var</span> <span class="n">address</span> <span class="o">:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>    <span class="n">get</span><span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">_address</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="n">set</span><span class="p">(</span><span class="n">newAddress</span><span class="p">){</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">newAddress</span> <span class="o">!=</span> <span class="n">_address</span><span class="p">{</span>
</span><span class='line'>            <span class="n">self</span><span class="p">.</span><span class="n">_address</span> <span class="o">=</span> <span class="n">newAddress</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h3>二.完整示例</h3>

<h4>1.QBaseClass overview</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">class</span> <span class="n">QBaseClass</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//read only</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">name:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">name</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//read/write Setters/Getters用来计算和获取属性，</span>
</span><span class='line'>    <span class="c1">//并没有给成员变量实例分配存储,所以，会触发EXC_BAD_ACCESS.</span>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">realAge:</span><span class="n">Int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">age:</span><span class="n">Int</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">realAge</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">set</span><span class="p">{</span>
</span><span class='line'>            <span class="n">realAge</span> <span class="o">=</span> <span class="n">newValue</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//read/write  with backing store，</span>
</span><span class='line'>    <span class="c1">//类似 @property(nonatomic, assign)NSInter number;变量自动为_number</span>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">_number:</span><span class="n">UInt</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="n">number</span> <span class="o">:</span><span class="n">UInt</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">_number</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="n">set</span><span class="p">(</span><span class="n">newNumber</span><span class="p">){</span>
</span><span class='line'>            <span class="k">if</span> <span class="n">newNumber</span> <span class="o">!=</span> <span class="n">number</span><span class="p">{</span>
</span><span class='line'>                <span class="n">self</span><span class="p">.</span><span class="n">_number</span> <span class="o">=</span> <span class="n">newNumber</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//read/write  with backing store,信息隐藏，监控属性变化，附加额外处理</span>
</span><span class='line'>     <span class="c1">//类似 @property(nonatomic, retain)NSString address;变量自动为_address</span>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="n">_address</span> <span class="o">:</span><span class="n">String</span> <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">{</span>
</span><span class='line'>        <span class="n">willSet</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">println</span><span class="p">(</span><span class="s">&quot;old Value = \(_address) will clear,new value = \(newValue) will set&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">self</span><span class="p">.</span><span class="n">_address</span> <span class="o">=</span> <span class="n">newValue</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="n">didSet</span><span class="p">{</span>
</span><span class='line'>            <span class="c1">//可以追回一些额外的处理逻辑</span>
</span><span class='line'>             <span class="n">println</span><span class="p">(</span><span class="s">&quot;Property did set&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="n">address</span> <span class="o">:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">_address</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="n">set</span><span class="p">(</span><span class="n">newAddress</span><span class="p">){</span>
</span><span class='line'>            <span class="k">if</span> <span class="n">newAddress</span> <span class="o">!=</span> <span class="n">_address</span><span class="p">{</span>
</span><span class='line'>                <span class="n">self</span><span class="p">.</span><span class="n">_address</span> <span class="o">=</span> <span class="n">newAddress</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(){</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.调用</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">var</span> <span class="nl">baseClass:</span><span class="n">QBaseClass</span> <span class="o">=</span> <span class="n">QBaseClass</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//baseClass.name = &quot;ksnowlv&quot;//报错</span>
</span><span class='line'><span class="n">baseClass</span><span class="p">.</span><span class="n">address</span> <span class="o">=</span> <span class="s">&quot;北京海淀区&quot;</span>
</span><span class='line'><span class="n">baseClass</span><span class="p">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>
</span><span class='line'><span class="n">baseClass</span><span class="p">.</span><span class="n">number</span> <span class="o">=</span> <span class="mi">0917</span><span class="p">;</span>
</span><span class='line'><span class="n">println</span><span class="p">(</span><span class="s">&quot;address =\(baseClass.address),age =\(baseClass.age),number =\(baseClass.number)&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<h4>3.日志输出</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">old</span> <span class="n">Value</span> <span class="o">=</span>  <span class="n">will</span> <span class="n">clear</span><span class="p">,</span><span class="n">new</span> <span class="n">value</span> <span class="o">=</span> <span class="err">北京海淀区</span> <span class="n">will</span> <span class="n">set</span>
</span><span class='line'><span class="n">Property</span> <span class="n">did</span> <span class="n">set</span>
</span><span class='line'><span class="n">address</span> <span class="o">=</span><span class="err">北京海淀区</span><span class="p">,</span><span class="n">age</span> <span class="o">=</span><span class="mi">20</span><span class="p">,</span><span class="n">number</span> <span class="o">=</span><span class="mi">917</span>
</span></code></pre></td></tr></table></div></figure>


<h3>三.小结</h3>

<p> 与<code>objective-c</code>中属性方法的不同之处：</p>

<ul>
<li><p>1.生成属性方法实例的方式不同。</p>

<blockquote><p>1.<code>objective-c</code>中属性方法会自动为属性方法生成实例变量，为实例变量分配内存。</p>

<p>2.而<code>swift</code>中属性方法仅仅是设置属性和获取属性，实例变量需要手动分配。</p></blockquote></li>
<li><p>2.属性方法使用便捷性不同。</p>

<blockquote><p><code>objective-c</code>的属性方法比较方便</p></blockquote></li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/09/swift-lei-shu-xing-slash-fang-fa/">Swift类属性/方法</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-09T11:22:00+08:00" pubdate data-updated="true">Sep 9<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/09/swift-lei-shu-xing-slash-fang-fa/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>swift类的属性<strong>setter/getter</strong>，普通方法，类方法/变量，访问权限等。</p>

<h4>1.setter/getter方法</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'>
</span><span class='line'> <span class="n">var</span> <span class="err">变量名</span><span class="o">:</span><span class="err">变量类型</span><span class="p">{</span>
</span><span class='line'>     <span class="n">get</span><span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="err">变量名</span><span class="p">;</span>
</span><span class='line'>     <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>     <span class="n">set</span><span class="p">{</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="err">变量名</span> <span class="o">=</span> <span class="n">newValue</span><span class="p">;</span>
</span><span class='line'>     <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>2.类方法和变量</h4>

<p>使用<code>class</code>修饰的方法或变量</p>

<p><code>类变量</code></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">class</span> <span class="n">var</span> <span class="err">变量名</span><span class="o">:</span> <span class="err">类型</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">get</span><span class="p">{</span>
</span><span class='line'>     <span class="k">return</span>  <span class="err">类名</span><span class="p">.</span><span class="err">变量名</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>类方法</code></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">class</span> <span class="n">func</span> <span class="err">方法名</span><span class="o">-&gt;</span><span class="err">参数</span><span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="err">类名</span><span class="p">.</span><span class="err">类变量</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>3.常方法。使用final修饰的方法</h4>

<p> <code>final</code> func 方法名</p>

<h4>4.访问权限</h4>

<blockquote><ul>
<li>类/方法/变量可以由关键字<strong>public</strong>/<strong>internal</strong>/<strong>private</strong>.</li>
<li>系统默认为<code>internal</code>,<code>private</code>可用于隐藏一些实现细节。</li>
</ul>
</blockquote>

<p><code>public</code> 公有</p>

<pre><code>标记为public的代码块一般用来建立API，这是最开放的权限，使得任何人只要导入这个模块，都可以访问使用。
</code></pre>

<p><code>internal</code> 内部</p>

<pre><code>标记为internal的代码块，在整个应用（App bundle）或者框架（framework）的范围内都是可以访问的。
</code></pre>

<p><code>private</code> 私有</p>

<pre><code>在哪里写的，就在哪里用。无论是类、变量、常量还是函数，一旦被标记为私有的，就只能在定义他们的源文件里使用，不能为别的文件所用。
</code></pre>

<h4>5.willSet/didSet</h4>

<p>这两个属性用来监视属性的除初始化之外的属性值变化情况。</p>

<p>示例代码如下。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">class</span> <span class="n">QPerson</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">///类的全局变量</span>
</span><span class='line'>    <span class="n">class</span> <span class="n">var</span> <span class="nl">sharedPersonNumber:</span> <span class="n">UInt</span> <span class="p">{</span>
</span><span class='line'>        <span class="c1">// 这里返回一个 UInt 值</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span>  <span class="n">QPerson</span><span class="p">.</span><span class="n">sharedPersonNumber</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">set</span><span class="p">{</span>
</span><span class='line'>            <span class="n">QPerson</span><span class="p">.</span><span class="n">sharedPersonNumber</span> <span class="o">=</span> <span class="n">newValue</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="cm">/**</span>
</span><span class='line'><span class="cm">    类方法</span>
</span><span class='line'><span class="cm">    :returns: 返回人员类的人数</span>
</span><span class='line'><span class="cm">    */</span>
</span><span class='line'>    <span class="n">final</span> <span class="n">class</span> <span class="n">func</span> <span class="n">countPerson</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">UInt</span><span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">QPerson</span><span class="p">.</span><span class="n">sharedPersonNumber</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">/// 普通成员变量</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_address:</span><span class="n">String</span><span class="p">;</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">_phoneNumber:</span><span class="n">String</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">/// setter/getter</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">name:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">name</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">set</span><span class="p">{</span>
</span><span class='line'>            <span class="n">self</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">newValue</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>    <span class="c1">/// setter/getter</span>
</span><span class='line'>    <span class="n">private</span> <span class="n">var</span> <span class="nl">_number:</span><span class="n">String</span><span class="p">{</span>
</span><span class='line'>        <span class="n">get</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">_number</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">set</span><span class="p">(</span><span class="n">number</span><span class="p">){</span>
</span><span class='line'>            <span class="n">self</span><span class="p">.</span><span class="n">_number</span> <span class="o">=</span> <span class="n">number</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">var</span> <span class="nl">age:</span> <span class="n">Int</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">willSet</span><span class="p">(</span><span class="n">age</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="n">NSLog</span><span class="p">(</span><span class="s">&quot;old age = \(self.age),new age = \(age)&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="n">didSet</span><span class="p">(</span><span class="n">age</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="n">NSLog</span><span class="p">(</span><span class="s">&quot;new age = \(self.age)&quot;</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">init</span><span class="p">(</span><span class="nl">name:</span><span class="n">String</span><span class="p">,</span> <span class="nl">number:</span><span class="n">String</span><span class="p">,</span> <span class="nl">address:</span><span class="n">NSString</span><span class="p">,</span> <span class="nl">phoneNumber:</span><span class="n">NSString</span><span class="p">){</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">_address</span> <span class="o">=</span> <span class="n">address</span><span class="p">;</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">_phoneNumber</span> <span class="o">=</span> <span class="n">phoneNumber</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">deinit</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/09/08/swift-ui-kong-jian-chu-ti-yan/">swift-UI-控件初体验</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-08T20:26:00+08:00" pubdate data-updated="true">Sep 8<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/09/08/swift-ui-kong-jian-chu-ti-yan/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>现在我们体验<strong>swift</strong>下UI控件的使用</h3>

<blockquote><ul>
<li><strong>UIButton</strong></li>
<li><strong>UIImageView</strong></li>
<li><strong>UILabel</strong></li>
<li><strong>UITableView</strong></li>
</ul>
</blockquote>

<h4>1.<strong>ViewController</strong>继承<code>UITableViewDataSource</code>，<code>UITableViewDelegate</code>，重写相应方法。</h4>

<h4>2.声明<code>UIButton</code>/<code>UIImageView</code>/<code>UILabel</code>/UITableView四种控件和列表数据数组，定义按钮事件。</h4>

<h4>3.控件及事件在IB连好。</h4>

<p><img src="/images/post/2014-09-08-swift-ui-kong-jian-chu-ti-yan/swift_ui_overviw_ib.png" alt="image" /></p>

<h4>完整代码</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="n">class</span> <span class="nl">ViewController:</span> <span class="n">UIViewController</span><span class="p">,</span><span class="n">UITableViewDataSource</span><span class="p">,</span><span class="n">UITableViewDelegate</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="err">@</span><span class="kt">IBOutlet</span> <span class="n">weak</span> <span class="n">var</span> <span class="nl">myButton:</span> <span class="n">UIButton</span><span class="o">!</span>
</span><span class='line'>    <span class="err">@</span><span class="kt">IBOutlet</span> <span class="n">weak</span> <span class="n">var</span> <span class="nl">imageView:</span> <span class="n">UIImageView</span><span class="o">!</span>
</span><span class='line'>    <span class="err">@</span><span class="kt">IBOutlet</span> <span class="n">weak</span> <span class="n">var</span> <span class="nl">label:</span><span class="n">UILabel</span><span class="o">!</span>
</span><span class='line'>    <span class="err">@</span><span class="kt">IBOutlet</span> <span class="n">weak</span> <span class="n">var</span> <span class="nl">tableView:</span><span class="n">UITableView</span><span class="o">!</span>
</span><span class='line'>    <span class="n">var</span> <span class="nl">list:</span><span class="n">NSArray</span><span class="o">!</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">override</span> <span class="n">func</span> <span class="n">viewDidLoad</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">super</span><span class="p">.</span><span class="n">viewDidLoad</span><span class="p">()</span>
</span><span class='line'>        <span class="c1">// Do any additional setup after loading the view, typically from a nib.</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">var</span> <span class="n">image</span> <span class="o">:</span> <span class="n">UIImage</span> <span class="o">=</span> <span class="n">UIImage</span><span class="p">(</span><span class="nl">named:</span><span class="s">&quot;qq_icon.jpg&quot;</span><span class="p">);</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">list</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;kair&quot;</span><span class="p">,</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span><span class="s">&quot;snow&quot;</span><span class="p">,</span><span class="s">&quot;air&quot;</span><span class="p">,</span><span class="s">&quot;rain&quot;</span><span class="p">,</span><span class="s">&quot;krain&quot;</span><span class="p">,</span><span class="s">&quot;cloud&quot;</span><span class="p">,</span><span class="s">&quot;kcloud&quot;</span><span class="p">];</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">imageView</span><span class="p">.</span><span class="n">image</span> <span class="o">=</span> <span class="n">image</span><span class="p">;</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">text</span> <span class="o">=</span> <span class="s">&quot;ksnowlv&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">override</span> <span class="n">func</span> <span class="n">didReceiveMemoryWarning</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">super</span><span class="p">.</span><span class="n">didReceiveMemoryWarning</span><span class="p">()</span>
</span><span class='line'>        <span class="c1">// Dispose of any resources that can be recreated.</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="err">@</span><span class="kt">IBAction</span> <span class="n">func</span> <span class="n">buttonClicked</span><span class="p">(</span><span class="nl">sender:</span> <span class="n">UIButton</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="c1">// The tag for each color was set in the storyboard for each button based</span>
</span><span class='line'>        <span class="c1">// on the type of color.</span>
</span><span class='line'>        <span class="n">var</span> <span class="nl">alertView:</span><span class="n">UIAlertView</span> <span class="o">=</span> <span class="n">UIAlertView</span><span class="p">(</span><span class="nl">title:</span> <span class="s">&quot;swift UIAlertView&quot;</span><span class="p">,</span> <span class="nl">message:</span> <span class="s">&quot;按钮被按下&quot;</span><span class="p">,</span> <span class="nl">delegate:</span> <span class="nb">nil</span><span class="p">,</span> <span class="nl">cancelButtonTitle:</span> <span class="s">&quot;ok&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">//title: String?, message: String?, delegate: AnyObject?, cancelButtonTitle: String?</span>
</span><span class='line'>        <span class="n">alertView</span><span class="p">.</span><span class="n">show</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">tableView</span><span class="p">(</span><span class="nl">tableView:</span> <span class="n">UITableView</span><span class="p">,</span> <span class="n">numberOfRowsInSection</span> <span class="nl">section:</span> <span class="n">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Int</span><span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">list</span><span class="p">.</span><span class="n">count</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">tableView</span><span class="p">(</span><span class="nl">tableView:</span> <span class="n">UITableView</span><span class="p">,</span> <span class="n">cellForRowAtIndexPath</span> <span class="nl">indexPath:</span> <span class="n">NSIndexPath</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">UITableViewCell</span><span class="p">{</span>
</span><span class='line'>        <span class="n">let</span> <span class="n">KTableViewCellIdentifier</span> <span class="o">=</span> <span class="s">&quot;tableViewCellIdentifier&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">var</span> <span class="n">cell</span> <span class="o">=</span> <span class="n">tableView</span><span class="p">.</span><span class="n">dequeueReusableCellWithIdentifier</span><span class="p">(</span><span class="n">KTableViewCellIdentifier</span><span class="p">)</span> <span class="n">as</span> <span class="n">UITableViewCell</span><span class="o">!</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">cell</span> <span class="o">==</span> <span class="nb">nil</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="n">cell</span> <span class="o">=</span> <span class="n">UITableViewCell</span><span class="p">(</span><span class="nl">style:</span><span class="p">.</span><span class="n">Default</span><span class="p">,</span> <span class="nl">reuseIdentifier:</span> <span class="n">KTableViewCellIdentifier</span><span class="p">)</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">var</span> <span class="n">rowIndex</span> <span class="o">=</span> <span class="n">indexPath</span><span class="p">.</span><span class="n">row</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">rowIndex</span> <span class="o">&lt;</span> <span class="n">self</span><span class="p">.</span><span class="n">list</span><span class="p">.</span><span class="n">count</span><span class="p">{</span>
</span><span class='line'>            <span class="n">cell</span><span class="p">.</span><span class="n">textLabel</span><span class="o">?</span><span class="p">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">self</span><span class="p">.</span><span class="n">list</span><span class="p">.</span><span class="n">objectAtIndex</span><span class="p">(</span><span class="n">rowIndex</span><span class="p">)</span> <span class="n">as</span> <span class="n">NSString</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">cell</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">func</span> <span class="n">tableView</span><span class="p">(</span><span class="nl">tableView:</span> <span class="n">UITableView</span><span class="p">,</span> <span class="n">heightForRowAtIndexPath</span> <span class="nl">indexPath:</span> <span class="n">NSIndexPath</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">CGFloat</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">self</span><span class="p">.</span><span class="n">tableView</span> <span class="o">==</span> <span class="n">tableView</span><span class="p">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="mi">50</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="mi">40</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h4>效果图</h4>

<p><img src="/images/post/2014-09-08-swift-ui-kong-jian-chu-ti-yan/swift_run_overview.png" alt="image" /></p>

<h4>小结</h4>

<pre><code>和objective-c下使用方法类似，优化方法大体相同，你原来的开发经验依然是很有用的。
</code></pre>
</div>
  
  


    </article>
  
  <div class="pagination">
    
      <a class="prev" href="/blog/page/6/">&larr; Older</a>
    
    <a href="/blog/archives">Blog Archives</a>
    
    <a class="next" href="/blog/page/4/">Newer &rarr;</a>
    
  </div>
</div>
<aside class="sidebar">
  
    <section>
  <h1>关于我</h1>
  <p>律威,QQ:309235009 微信:ksnowlv</p>
  <p>手机单机游戏/手机网游封神online/手机阅读/腾讯iOS地图客户端&实时公交&开放API(地图&街景)/iPad/iPhone淘宝客户端及性能优化</p>
  <p>目前在银客做银客/简理财/&#8230;</p>
  <p>能做一点,是一点;不求大成,不求一万年;积少成多,水到渠成!</p>
</section>
<section>
  <h1>最近发布</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2017/04/26/android-drawable/">Android drawable</a>
      </li>
    
      <li class="post">
        <a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/">iOS下使用ffmpeg</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/">iOS有限后台任务</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/android-xia-jie-ya-suo/">Android下gzip数据流解压缩</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/facebook-infer-de-shi-yong/">FaceBook-infer的使用</a>
      </li>
    
  </ul>
</section>
<section>
  <h1>分类</h1>
    <ul id="category-list"><li><a href='/blog/categories/android-jie-ya-suo/'>Android-解压缩 (1)</a></li><li><a href='/blog/categories/androidduo-xian-cheng/'>Android多线程 (3)</a></li><li><a href='/blog/categories/androidcha-jian/'>Android插件 (1)</a></li><li><a href='/blog/categories/androidhui-tu-./'>Android绘图。 (1)</a></li><li><a href='/blog/categories/androidwang-luo/'>Android网络 (1)</a></li><li><a href='/blog/categories/git/'>git (6)</a></li><li><a href='/blog/categories/githubbo-ke/'>github博客 (8)</a></li><li><a href='/blog/categories/ios/'>iOS (6)</a></li><li><a href='/blog/categories/ios-block/'>iOS-block (1)</a></li><li><a href='/blog/categories/ios-bug/'>iOS-Bug (2)</a></li><li><a href='/blog/categories/ios-memory/'>iOS-Memory (4)</a></li><li><a href='/blog/categories/ios-push/'>iOS-push (1)</a></li><li><a href='/blog/categories/ios-ui/'>iOS-UI (2)</a></li><li><a href='/blog/categories/ios-you-hua/'>iOS-优化 (1)</a></li><li><a href='/blog/categories/ios-nei-cun/'>iOS-内存 (1)</a></li><li><a href='/blog/categories/ios-ji-chu/'>iOS-基础 (3)</a></li><li><a href='/blog/categories/ios-duo-xian-cheng/'>iOS-多线程 (3)</a></li><li><a href='/blog/categories/ios-cun-chu/'>iOS-存储 (1)</a></li><li><a href='/blog/categories/ios-an-quan/'>iOS-安全 (1)</a></li><li><a href='/blog/categories/ios-wang-luo/'>iOS-网络 (13)</a></li><li><a href='/blog/categories/ios-jie-ya-suo/'>iOS-解压缩 (1)</a></li><li><a href='/blog/categories/ios-she-ji-mo-shi/'>iOS-设计模式 (1)</a></li><li><a href='/blog/categories/ios-diao-shi/'>iOS-调试 (1)</a></li><li><a href='/blog/categories/iosyou-hua/'>iOS优化 (1)</a></li><li><a href='/blog/categories/iosji-chu/'>iOS基础 (2)</a></li><li><a href='/blog/categories/iosyin-pin/'>iOS音频 (1)</a></li><li><a href='/blog/categories/mac/'>Mac (4)</a></li><li><a href='/blog/categories/mac-duo-xian-cheng/'>Mac-多线程 (1)</a></li><li><a href='/blog/categories/nginx/'>nginx (3)</a></li><li><a href='/blog/categories/php/'>php (3)</a></li><li><a href='/blog/categories/python/'>python (12)</a></li><li><a href='/blog/categories/swift/'>swift (11)</a></li><li><a href='/blog/categories/uml/'>UML (8)</a></li><li><a href='/blog/categories/xcode/'>Xcode (11)</a></li><li><a href='/blog/categories/chan-pin-she-ji/'>产品设计 (2)</a></li><li><a href='/blog/categories/gong-ju/'>工具 (2)</a></li><li><a href='/blog/categories/shu-ju-ku/'>数据库 (2)</a></li><li><a href='/blog/categories/sheng-huo-pian-duan/'>生活片段 (10)</a></li><li><a href='/blog/categories/zhi-ye-sheng-ya/'>职业生涯 (7)</a></li><li><a href='/blog/categories/du-shu/'>读书 (2)</a></li></ul>
</section>
<section>
  <h1>外部链接</h1>
    <ul id="外部链接">
    	<li><a href="https://developer.apple.com">苹果开发者中心</a> </li>
    	<li><a href="http://stackoverflow.com/questions/tagged/ios">stackoverflow</a></li>
    	<li><a href="http://ksnowlv.blog.163.com/blog/#m=0">ksnowlv网易博客</a></li>
	</ul>
</section>

  
</aside>

    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 律威 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>

  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253538110'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/z_stat.php%3Fid%3D1253538110%26online%3D1' type='text/javascript'%3E%3C/script%3E"));</script>

  <script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F4c1727a94ea20751730e8a556dbdbf82' type='text/javascript'%3E%3C/script%3E"));
</script>

</p>



</footer>
  

<script type="text/javascript">
      var disqus_shortname = 'ksnowlv';
      
        
        var disqus_script = 'count.js';
      
    (function () {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    }());
</script>








</body>
</html>
